//
//  CZStatusCell.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/27.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
import SnapKit

class CZStatusCell: UITableViewCell {
    // 纪录底部toolbar顶部约束
    var toolBarConstraint: Constraint?
    //模型
    var status: CZStatus? {
        didSet {
           originalView.status = status
            originalToolbar.status = status
            //卸载约束
            toolBarConstraint?.uninstall()
            if status?.retweeted_status == nil {
                relayView.hidden = true
                originalToolbar.snp_makeConstraints(closure: { (make) in
                    toolBarConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            } else {
                relayView.status = status
                relayView.hidden = false
                originalToolbar.snp_makeConstraints(closure: { (make) in
                    toolBarConstraint = make.top.equalTo(relayView.snp_bottom).constraint
                })
            }
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(relayView)
        contentView.addSubview(originalToolbar)
        
        //添加约束
        originalView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
        }
        relayView.snp_makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        originalToolbar.snp_makeConstraints { (make) in
            make.right.left.equalTo(contentView)
            toolBarConstraint = make.top.equalTo(relayView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(originalToolbar.snp_bottom)
        }
    }
    // MARK - 懒加载控件
    //原创微博
    private lazy var originalView:CZOrigunalView = CZOrigunalView()
    //转发微博
    private lazy var relayView: CZRelayView = CZRelayView()
    //底部toolbar
    private lazy var originalToolbar: CZToolbar = CZToolbar()
}
